<?php
/**
 * Parterns Grid
 *
 */

use Fox\Ddic\DataElement\Formater\Void;
use Fox\Grid\FieldCat\Field\Renderer\Token;
use Fox\Behaviors;
use Fox\Grid\FieldCat\Field\Renderer\Image;
use Nawa\Customer;
use Fox\Grid\FieldCat\Field\Renderer\Anchor;
use Fox\Document;
use Fox\Dao\SqlFragment;
use Nawa\MedicalStudy\Role;
use Fox\Generator;
use Nawa\Messages;
use Fox\Html\Button;
use Fox\Grid;
use Fox\Html\Block;
$xview = Xview::instance();
$keys = $xview->val(array('msid','mcid','monitoringid'));
$block = new Block('bPartners',Msg(Messages::BUNDLE,Messages::LABEL_INVESTIGATORS));
$block->collapsed(false);

$grid = new Grid('gPartners',Generator::url('medicalstudies','partners',null,
							array_merge($keys,array('reload' => true))));
$grid->postContentOnReload(true);
$grid->fieldcat(array('customerid','name1','role','medicalcenters','docid','ms_resume','bupload'));
$grid->fieldcat()->setFieldAttr(array('customerid','medicalcenters','docid','name1','role'), 'SetHiddenInput',true);

$roles = Role::all();
$grid->fieldcat()->field('role')
	->headerAlign('center')
	->align('center')
	->input()
	->inputAsDropList(false,false,true)
	->inputRenderer()
	->options($roles,'role','label');
if(!empty($keys['mcid'])) {
	foreach($roles as $role) {
		$rolesLabel[$role->role] = $role->label;
	}
	$grid->fieldcat()->field('role')
		->setGenerator(Generator::useFunction(function($data) use ($rolesLabel) {
			if(isset($rolesLabel[$data['role']])) {
				return $rolesLabel[$data['role']];
			}
			return $data['role'];
		}))
		->input(false)
		->SetHiddenInput(true);
}
$grid->fieldcat()->field('customerid')->hidden();
$grid->fieldcat()->field('docid')->hidden();
$grid->fieldcat()->field('name1')
	->width(10)
	->renderers(new Anchor(Generator::url('contacts','update',null,array(
		'customerid' => Generator::useElement('customerid'))
	)));
$grid->fieldcat()->field('medicalcenters')
	->width(20)
	->renderers(new Token(Generator::url('medicalstudies','partners','unassign',array(
												'msid' => $keys['msid'],
												'mcid' => Generator::useElement('mcid'),
												'customerid' => Generator::useElement('customerid')))))
	->formater(new Void());
if(!empty($keys['monitoringid'])) {
	$grid->fieldcat()->field('medicalcenters')->noOutput();
}
$grid->fieldcat()->field('bupload')->renderers(new Image())
	->setGenerator(Generator::display('resume', Generator::url('contacts','document','upload',
					array('customerid' => Generator::useElement('customerid'),
										  'doctype' => Customer::DOC_RESUME,
											'caller' => 'MC_gPartners',
											'msid' => $keys['msid'])
					)))
	->label(Msg(Messages::BUNDLE,Messages::BUT_IMPORT_RESUME));
	
$grid->fieldcat()->field('ms_resume')->setGenerator(
	Generator::useFunction(function($data){
		if(!empty($data['docid'])) {
			$docid = $data['docid'];
		} else {
			$customer = Fox\Dao::load(Customer::className(),$data['customerid']);
			$resume = $customer->resume();
			if(!empty($resume)) {
				$docid = $resume->docid;
			}
		}
		if(!empty($docid)) {
			return Generator::generateValue(Fox\Generator::url(
						'core',
						'files',
						'download',
						array('id' => $docid,'xvtrs' => Generator::useValue('none'))),
						$data);
		}
		return null;
	})
);
$grid->setCoche();
$grid->fieldcat()->behaviors(Fox\Behaviors::key(Fox\Generator::useElement('customerid')));
$grid->footer()->add(new Button(Grid::BUTTON_ADD_RECORD,Generator::display('addPartners',
						Generator::url('medicalstudies','partners','add',$keys),
											'MC_gPartners')));
$grid->footer()->add(new Button(Grid::BUTTON_DELETE_LINE,
	Generator::gridDeleteLines('gPartners'),Msg(Fox\Msg::BUNDLE,Fox\Msg::BUT_DELETE)));

$grid->footer()->add(new Button('assign','?',Msg(Messages::BUNDLE,Messages::ASSIGN_TO_CENTERS)))
	->behaviors(Behaviors::action(
		Generator::url('medicalstudies','partners','medicalcenters',
							$keys),true,
							'#tb_gPartners tr input:checked',
							'tr'));
$block->add($grid);
return $block;